package com.whfc.fvs.service.queue;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whfc.common.constant.QueueConst;
import com.whfc.fvs.dao.FvsDeviceMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-09-06 18:08
 */
@Component
@RabbitListener(queues = QueueConst.MACH_FVS_GPS_DATA)
public class FvsMachGpsQueueMessageListener {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FvsDeviceMapper fvsDeviceMapper;

    @RabbitHandler
    public void process(String msg) {
        logger.info("设备定位数据, msg:{}", msg);
        try {
            JSONObject object = JSON.parseObject(msg);
            if (object == null) {
                return;
            }
            Integer deviceId = object.getInteger("deviceId");
            if (deviceId == null) {
                return;
            }
            //更新设备定位
            Double lng = object.getDouble("lng");
            Double lat = object.getDouble("lat");
            fvsDeviceMapper.updateDeviceGpsByMachDeviceId(deviceId, lng, lat);
        } catch (Exception e) {
            logger.error("监控定位数据处理失败.", e);
        }
    }

}
